/**
 * Created by Administrator on 2019/5/21/021.
 */
import CZML_GENERALTOOLS_CLASS from "./../main/CZML_GENERALTOOLS_CLASS.js"

class PRIMITIVE_SpacecraftAnimatedModel {
    constructor(viewer, id, ModelId, ModelUrl, mode, speedup, index, orbitStartTime, loop, StartTime) {
        this._viewer = viewer
        this.id = id
        this.ModelId = ModelId
        this.ModelUrl = ModelUrl
        this.mode = mode
        this.speedup = speedup
        this.index = index
        this.orbitStartTime = orbitStartTime
        this.loop = loop
        this.StartTime = StartTime
        this.PRIMITIVE_AddSpacecraftAnimatedModel()
    }

    PRIMITIVE_AddSpacecraftAnimatedModel() {
        const viewer = this._viewer
        const id = this.id
        const speedup = this.speedup
        const index = this.index
        const model = viewer.scene.primitives.add(Cesium.Model.fromGltf({
            id: this.ModelId,
            url: this.ModelUrl,
            show: true,
            scale: 0.2,
            minimumPixelSize: 16,
            maximumScale: 2000,
            allowPicking: true
        }))

        let loop = this.loop
        if (loop) { loop = Cesium.ModelAnimationLoop.REPEAT } else { loop = Cesium.ModelAnimationLoop.NONE }

        const mode = this.mode
        const staTime = Cesium.JulianDate.fromIso8601(CZML_GENERALTOOLS_CLASS.Utcg2ISO8601TimeTransformation(this.StartTime))
        if (mode === 1) {
            model.readyPromise.then(function (model) {
                model.activeAnimations.addAll({
                    startTime: staTime,
                    speedup: speedup,
                    loop: loop
                })
            })
        }
        if (mode === 0) {
            model.readyPromise.then(function (mod) {
                mod.activeAnimations.add({
                    startTime: staTime,
                    speedup: speedup,
                    index: index,
                    loop: loop
                })
            })
        }
        const orbitStartTime1 = CZML_GENERALTOOLS_CLASS.Utcg2ISO8601TimeTransformation(this.orbitStartTime)
        const iniTime = Cesium.JulianDate.fromIso8601(orbitStartTime1)
        const addEvent = function (scene, time) {
            if (viewer.clock._currentTime.secondsOfDay >= iniTime.secondsOfDay && viewer.clock._currentTime.dayNumber >= iniTime.dayNumber) {
                const relSecs = (viewer.clock._currentTime.dayNumber - iniTime.dayNumber) * 86400 + (viewer.clock._currentTime.secondsOfDay - iniTime.secondsOfDay)
                const position = CZML_GENERALTOOLS_CLASS.FindModelObjectInformation(id, relSecs)
                if (position) {
                    const position1 = new Cesium.Cartesian3(position[0], position[1], position[2])
                    const AttitudeDegree = CZML_GENERALTOOLS_CLASS.FindModelAttitudeInformation(id, relSecs)
                    // var hprx = AttitudeDegree.pitch + Cesium.Math.toRadians(0); //x
                    // var hpry = AttitudeDegree.heading + Cesium.Math.toRadians(0);  //y
                    // var hprz = AttitudeDegree.roll + Cesium.Math.toRadians(0);  //z

                    const hpr = new Cesium.HeadingPitchRoll(AttitudeDegree.pitch, AttitudeDegree.heading, AttitudeDegree.roll)
                    const Matrix = Cesium.Transforms.headingPitchRollToFixedFrame(position1, hpr)
                    model.modelMatrix = Matrix
                }
            }
        }
        viewer.scene.preUpdate.addEventListener(addEvent, "addEvent")
        // 插入该数据对象
        const ret = CZML_GENERALTOOLS_CLASS.InsertModelObjectInformation(this.ModelId, model, true)
        return ret
    }
}
export default PRIMITIVE_SpacecraftAnimatedModel
